# DS Kanji Flash
# Copyright (C) 2009,2010 Bernhard Schelling
#
# This file is part of DS Kanji Flash.
#
# DS Kanji Flash is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# DS Kanji Flash is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with DS Kanji Flash.  If not, see <http://www.gnu.org/licenses/>.

include $(DEVKITARM)/ds_rules

#---------------------------------------------------------------------------------
.SUFFIXES:
#---------------------------------------------------------------------------------
GAME_SUBTITLE1	:= DS Kanji Flash
GAME_SUBTITLE2 	:= Learning Kanji for Non-Japanese
GAME_ICON	:= $(CURDIR)/../logo.bmp

#---------------------------------------------------------------------------------
# TARGET is the name of the output
# BUILD is the directory where object files & intermediate files will be placed
# SOURCES is a list of directories containing source code
# INCLUDES is a list of directories containing extra header files
#---------------------------------------------------------------------------------
TARGET		:=	$(shell basename $(CURDIR))
BUILD		:=	build
SOURCES		:=	source  
DATA		:=	data
INCLUDES	:=	include $(BUILD)

#---------------------------------------------------------------------------------
# options for code generation
#---------------------------------------------------------------------------------
ARCH	:=	-mthumb-interwork
CFLAGS	:=	-g -Wall -O2 -march=armv5te -mtune=arm946e-s -fomit-frame-pointer -ffast-math $(ARCH)
CFLAGS	+=	$(INCLUDE) -DARM9
CFLAGS	+= -ffunction-sections -fdata-sections
CXXFLAGS	:= $(CFLAGS) -fno-rtti -fno-exceptions
ASFLAGS	:=	-g $(ARCH)
LDFLAGS	=	-specs=ds_arm9.specs -g $(ARCH) -Wl,-Map,$(notdir $*.map) -Wl,--gc-sections

#---------------------------------------------------------------------------------
# any extra libraries we wish to link with the project
#---------------------------------------------------------------------------------
#LIBS	:= -lul -lpng -lz -lfat -lnds9 -lm
#LIBS	:= -lul -lpng -lfat -lnds9
LIBS	:= -lul -lnds9
 
#---------------------------------------------------------------------------------
# list of directories containing libraries, this must be the top level containing
# include and lib
#---------------------------------------------------------------------------------
LIBDIRS	:= $(LIBNDS) ..
 
#---------------------------------------------------------------------------------
ifeq ($(strip $(DEVKITARM)),)
	$(error "Please set DEVKITARM in your environment. export DEVKITARM=<path to>devkitARM")
endif
define bin2o
	bin2s $< | $(AS) -o $(@)
	echo "extern const u8" `(echo $(<F) | sed -e 's/^\([0-9]\)/_\1/' | tr . _)`"["`(stat -c%s $<)`"];" > `(echo $(<F) | tr . _)`.h
	echo "extern const u8" `(echo $(<F) | sed -e 's/^\([0-9]\)/_\1/' | tr . _)`"_end[];" >> `(echo $(<F) | tr . _)`.h
	echo "extern const u32" `(echo $(<F) | sed -e 's/^\([0-9]\)/_\1/' | tr . _)`_size";" >> `(echo $(<F) | tr . _)`.h
endef
ifneq ($(BUILD),$(notdir $(CURDIR)))
#---------------------------------------------------------------------------------
 
export OUTPUT	:= $(CURDIR)/$(TARGET)
 
export VPATH	:= $(foreach dir,$(SOURCES),$(CURDIR)/$(dir)) \
            	   $(foreach dir,$(DATA),$(CURDIR)/$(dir))
	
export DEPSDIR	:= $(CURDIR)/$(BUILD)

CFILES		:= $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.c)))
CPPFILES	:= $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.cpp)))
SFILES		:= $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.s)))
BINFILES	:= $(foreach dir,$(DATA),$(notdir $(wildcard $(dir)/*.bin)))
BINFILES	+= $(foreach dir,$(DATA),$(notdir $(wildcard $(dir)/*.z)))
BINFILES	+= $(foreach dir,$(DATA),$(notdir $(wildcard $(dir)/*.png)))
BINFILES	+= $(foreach dir,$(DATA),$(notdir $(wildcard $(dir)/*.txt)))
BINFILES	+= $(foreach dir,$(DATA),$(notdir $(wildcard $(dir)/*.oft)))
#BINFILES	+= $(addsuffix .oft, $(foreach dir,$(DATA),$(notdir $(basename $(wildcard $(dir)/*.bmp)))))

#---------------------------------------------------------------------------------
# use CXX for linking C++ projects, CC for standard C
#---------------------------------------------------------------------------------
ifeq ($(strip $(CPPFILES)),)
	export LD := $(CC)
else
	export LD := $(CXX)
endif
#---------------------------------------------------------------------------------

export OFILES	:= $(addsuffix .o, $(BINFILES)) $(CPPFILES:.cpp=.o) $(CFILES:.c=.o) $(SFILES:.s=.o)
 
export INCLUDE	:= $(foreach dir,$(INCLUDES),-I$(CURDIR)/$(dir)) \
		   $(foreach dir,$(LIBDIRS),-I$(dir)/include) \
		   $(foreach dir,$(LIBDIRS),-I$(dir)/include) \
		   -I$(CURDIR)/$(BUILD)

export LIBPATHS	:= $(foreach dir,$(LIBDIRS),-L$(dir)/lib)
 
.PHONY: $(BUILD) clean
 
$(BUILD):
	@[ -d $@ ] || mkdir -p $@
	@$(MAKE) --no-print-directory -C $(BUILD) -f $(CURDIR)/Makefile
	@if [ ! -e "Makefile.bak" ]; then cp Makefile Makefile.bak; fi
	@if [ "`(stat -c%s "Makefile")`" != "`(stat -c%s "Makefile.bak")`" ]; then cp Makefile Makefile.bak; fi

clean:
	@echo clean ...
	@rm -fr $(BUILD) $(TARGET).elf $(TARGET).nds $(TARGET).arm9 $(TARGET).ds.gba 
 
#---------------------------------------------------------------------------------
else
#---------------------------------------------------------------------------------
 
DEPENDS	:=	$(OFILES:.o=.d)
 
#---------------------------------------------------------------------------------
# main targets
#---------------------------------------------------------------------------------
$(OUTPUT).nds	: 	$(OUTPUT).arm9
$(OUTPUT).arm9	:	$(OUTPUT).elf
$(OUTPUT).elf	:	$(OFILES)

%.o: %.cpp
	@echo $(notdir $<)
	@echo $(CC) -MMD -MP -MF $(DEPSDIR)/$*.d $(CFLAGS) -c $< -o $@
	@$(CC) -MMD -MP -MF $(DEPSDIR)/$*.d $(CFLAGS) -c $< -o $@ 2>&1 | sed -e 's/^.*\?[\\\\\/]\([^\\\\\/]\+[\\\\\/][^\\\\\/:]*\):\([0-9][0-9]*\)/\1(\2) /g' 1>&2 && exit $${PIPESTATUS[0]}
%.elf:
	@echo linking $(notdir $@)
	@echo $(LD)  $(LDFLAGS) $(OFILES) $(LIBPATHS) $(LIBS) -o $@
	@$(LD)  $(LDFLAGS) $(OFILES) $(LIBPATHS) $(LIBS) -o $@ 2>&1 | sed -e 's/^.*\?[\\\\\/]\([^\\\\\/]\+[\\\\\/][^\\\\\/:]*\):\([0-9][0-9]*\)/\1(\2) /g' 1>&2 && exit $${PIPESTATUS[0]}

#---------------------------------------------------------------------------------
%.bin.o	:	%.bin
#---------------------------------------------------------------------------------
	@echo $(notdir $<)
	@$(bin2o)
	@if [ -e $(basename $(<)).h ]; then cp "$(basename $(<)).h" "$(notdir $(basename $(<))).h"; fi

#---------------------------------------------------------------------------------
%.png.o	:	%.png
#---------------------------------------------------------------------------------
	@echo $(notdir $<)
	@$(bin2o)

#---------------------------------------------------------------------------------
%.txt.o	:	%.txt
#---------------------------------------------------------------------------------
	@echo $(notdir $<)
	@$(bin2o)
	@if [ -e $(basename $(<)).h ]; then cp "$(basename $(<)).h" "$(notdir $(basename $(<))).h"; fi

#---------------------------------------------------------------------------------
%.z.o	:	%.z
#---------------------------------------------------------------------------------
	@echo $(notdir $<)
	@$(bin2o)

#---------------------------------------------------------------------------------
%.oft.o	:	%.oft
#---------------------------------------------------------------------------------
	@echo $(notdir $<)
	@$(bin2o)
#rm $<;

##---------------------------------------------------------------------------------
#%.oft	:	%.bmp
##---------------------------------------------------------------------------------
#	@echo $(notdir $<)
#	@-echo  `../font2oft -create "$(<)" "$(basename $(<)).ini" "$(@)"` >/dev/null
#	@-echo  `../font2oft -create "$(<)" "$(basename $(<)).ini" "$(basename $(<)).oft"` >/dev/null

-include $(DEPENDS)
 
#---------------------------------------------------------------------------------------
endif
#---------------------------------------------------------------------------------------
